﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ExtBusiness.OnLine;
using System.Data;
using ExtBusiness;
using System.Web.Security;
using System.Drawing;
using ExtBusiness.FlowOperation;
using ExtBusiness.DataCollect;
using ExtBusiness.User;

public partial class QueryAndCollect_JsscSpCollect : PageBase
{
    private RequestFlowOperation rfoGloab = new RequestFlowOperation();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlTjlb.DataBind();
            BindFlowName();
            BindMyHandled();
        }
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    }

    protected DataTable BindMyHandled()
    {
        string strWhere = string.Empty;
        
        if (!string.IsNullOrEmpty(ddlFlow.SelectedItem.Value))//流程名称
            strWhere += string.Format(" and w.wname = '{0}'", ddlFlow.SelectedItem.Value);

        string strDataWhere = string.Empty;
        if (!string.IsNullOrEmpty(ddlSteps.Text))
        {
            strDataWhere = string.Format(@" and i.iid in (select h.iid from st_work_item_hist h where h.step='{0}' ", ddlSteps.Text);

            if (!string.IsNullOrEmpty(ddlNames.Text))
            {
                strDataWhere += string.Format(@" and h.userid='{0}' ", ddlNames.SelectedValue);
            }
        }
        if (!string.IsNullOrEmpty(startDate.Value) || !string.IsNullOrEmpty(startDate.Value))
        {
            if (string.IsNullOrEmpty(strDataWhere))
            {
                strDataWhere = @" and i.iid in (select h.iid from st_work_item_hist h where h.submit_time is not null  ";
            }
            else
            {
                strDataWhere += @" and h.submit_time is not null ";
            }

            if (!string.IsNullOrEmpty(startDate.Value))//开始时间
                strDataWhere += string.Format(" and h.submit_time>=to_date('{0}','yyyy-mm-dd hh24-mi-ss')", startDate.Value);
            if (!string.IsNullOrEmpty(endDate.Value))//结束时间
                strDataWhere += string.Format(" and h.submit_time<=to_date('{0}','yyyy-mm-dd hh24-mi-ss')", endDate.Value);           
        }

        if (!string.IsNullOrEmpty(strDataWhere))
        {
            strWhere += strDataWhere + ")";
        }

        if (!string.IsNullOrEmpty(ddlTjlb.Text))//图件类别
            strWhere += string.Format(" and dtyt='{0}'", ddlTjlb.Text);

        DataTable dtTemp = rfoGloab.GetJszxDataCollect("", strWhere);
        //添加统计列
        List<string> lstTemp = new List<string>();
        lstTemp.Add("bjs");
        lstTemp.Add("tfzs");
        DataCollectQuery.AddCollectRow(dtTemp, "dtyt", lstTemp,false);
        this.gvMessage.DataSource = dtTemp;
        this.gvMessage.RecordCount = dtTemp.Rows.Count;
        this.gvMessage.DataBind();
        
        return dtTemp;
    }

    /// <summary>
    /// datagrid数据加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvMessage_OnLoadData(object sender, EventArgs e)
    {
        this.BindMyHandled();
    }

    protected void btnRefer_Click(object sender, EventArgs e)
    {
        BindMyHandled();
    }

    /// <summary>
    /// 绑定该用户有权限的流程名称
    /// </summary>
    /// <!--addby zhongjian 20100608-->
    public void BindFlowName()
    {
        ClsWorkFlow newWorkflow = new ClsWorkFlow();
        DataTable dtTable = newWorkflow.GetWorkFlowByUserId(UserId, "");
        ddlFlow.Items.Clear();
        ddlFlow.Items.Add(new ListItem("所有流程", ""));
        foreach (DataRow row in dtTable.Rows)
        {
            ddlFlow.Items.Add(new ListItem(row["wname"].ToString(), row["wname"].ToString()));
        }
    }

    /// <summary>
    /// 绑定岗位信息
    /// </summary>
    public void BindSteps()
    {
        if (!string.IsNullOrEmpty(ddlFlow.Text))
        {
            DataTable dtTable = rfoGloab.GetStepInFlows(ddlFlow.Text);
            ddlSteps.Items.Clear();
            foreach (DataRow row in dtTable.Rows)
            {
                ddlSteps.Items.Add(new ListItem(row["sname"].ToString(), row["sname"].ToString()));
            }
        }
    }

    /// <summary>
    /// 绑定用户信息
    /// </summary>
    public void BindUsers()
    {
        if (!string.IsNullOrEmpty(ddlFlow.Text) && !string.IsNullOrEmpty(ddlSteps.Text))
        {
            DataTable dtTable = UserGroupHandle.GetUserInfoByFlowStep(ddlFlow.Text, ddlSteps.Text);
            ddlNames.Items.Clear();

            ddlNames.Items.Add("");
            foreach (DataRow row in dtTable.Rows)
            {
                ddlNames.Items.Add(new ListItem(row["user_name"].ToString(), row["userid"].ToString()));
            }
        }
    }

    protected void ddlFlow_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSteps();
    }

    protected void ddlSteps_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindUsers();
    }
}